/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import flightlifter.FlightLifter;
import flightlifter.TrueTypeFont;
import java.lang.reflect.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;

/**
 * Container class for the applet's GUI
 */
public class GUI {

    //should probably encapsulate these variables as a look and feel class
    public float[] BACK = new float[]{0f, 0f, .1f, .9f};
    public float[] TITLE_FILL = new float[]{0f, 0f, .2f, .9f};
    public float[] BORDER = new float[]{0, 0, 0};
    public float[] FILL = new float[]{0f, 0f, 0f, .5f};
    public float[] TEXT = new float[]{1f, 1f, 1f, 1f};
    public float[] HIGHLIGHT = new float[]{1f, 1f, 0f, .5f};

    ;
    public float[] TITLE_TEXT = new float[]{1.f, 1.f, 0f, 1f};
    private int showGUI = 1;
    private int mouseConsumed;
    GUILayout rightPopup = null;
    GUILayout leftPopup = null;
    GUILayout middlePopup = null;
    FlightLifter parent;
    TrueTypeFont font;
    Vector guiLayouts = new Vector();
    GUIComponent focusedComp = null;



    /**
     *  Creates the general management container for all SGGUI components. Only one
     *  instance is necessarty per applet. Should only be instantiated after size()
     *  is called in the setup method.
     * @param parent the overarching Processing sketch
     * @param font to save on library size, the parent class is expected to manage
     * the current font. This could be pulled out of the parent param, but is
     * required explicitly to ensure that the object is created.
     *
     */
    public GUI(FlightLifter parent, TrueTypeFont font) {

        this.parent = parent;
        this.font = font;

        parent.registerMouseEvent(this);

    }

    /**
     * Sets the currently focused component.
     */
    private void setFocused(GUIComponent newFocus) {
        focusedComp = newFocus;
    }

    /**
     * Sets the currently focused component.
     */
    public GUIComponent getFocused() {
        return focusedComp;
    }

    /**
     * Adds a layout manager.  Each SGGUI can maintain an unlimited number of
     * layouts.
     */
    public void add(GUILayout layout) {
        guiLayouts.add(layout);
    }

    /**
     * Draws the GUI to the screen.  This is called automatically by the parent
     * applet, and should never be called by the user.
     */
    public void draw() {



        if (showGUI == 1) {

            GL11.glLineWidth(1.f);
            //GUIs work in screen space. Reset the camera matrix
            parent.set2DMode();

            GL20.glUseProgram(0);

            GL11.glEnable(GL11.GL_BLEND); // Enabled blending for text
            GL11.glEnable(GL11.GL_TEXTURE_2D); // Enabled blending for text
            GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

            GUILayout temp;
            for (int i = 0; i < guiLayouts.size(); i++) {
                temp = (GUILayout) guiLayouts.elementAt(i);
                temp.draw();
            }
        }
    }

    public int mouseClicked(int mouseX, int mouseY, int button) {
        GUILayout temp;
        for (int i = 0; i < guiLayouts.size(); i++) {
            temp = (GUILayout) guiLayouts.elementAt(i);
            if (temp.mouseClicked(mouseX, mouseY) == 1) {
                return 1;
            }
        }
        setFocused(null);
        return 0;

    }



    /**
     * Handles mouse pressed events. Should not be called by user.
     * @return integer indicating whether the GUI was affected by the mouseEvent
     */
    public int mousePressed(int mouseX, int mouseY, int button) {

        if (rightPopup != null && button == 1) {
            rightPopup.setVisible(1);
            rightPopup.setPosition(mouseX - 2, mouseY - 2);
            return 1;
        } else if (leftPopup != null && button == 0) {
            leftPopup.setVisible(1);
            leftPopup.setPosition(mouseX - 2, mouseY - 2);
            return 1;
        } else {
            GUILayout temp;
            for (int i = 0; i < guiLayouts.size(); i++) {
                temp = (GUILayout) guiLayouts.elementAt(i);
                if (temp.mousePressed(mouseX, mouseY) == 1) {
                    //TODO. hide mouse on pressed
                    //temp.setVisible(0);
                    return 1;
                }

            }
            setFocused(null);
            return 0;
        }


    }

    /**
     * Handles mouse drag events. Should not be called by user.
     * @return integer indicating whether the GUI was affected by the mouseEvent
     */
    public int mouseDragged(int mouseDX, int mouseDY) {

        if (focusedComp != null) {
            focusedComp.mouseDragged(mouseDX, mouseDY);
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * Handles mouse drag events. Should not be called by user.
     * @return integer indicating whether the GUI was affected by the mouseEvent
     */
    public void mouseMoved(int mouseX, int mouseY) {

        GUILayout temp;
        for (int i = 0; i < guiLayouts.size(); i++) {
            temp = (GUILayout) guiLayouts.elementAt(i);
            temp.mouseMoved(mouseX, mouseY);
        }
    }

    /**
     * Returns the mouseConsumed variable, indicating whether a recent mouse
     * event affected the GUI.  This variable should be checked within the
     * Processing sketch if there are any calls to mouseDragged or mouseClicked
     * to see whether the mouseEvent was targeted for the GUI or the applet.
     *
     * @return integer indicating whether the GUI was affected by the mouseEvent
     */
    public int getMouseConsumed() {
        return mouseConsumed;
    }

    public void setRightPopup(GUILayout popup) {

        rightPopup = popup;
        rightPopup.setVisible(0);
        rightPopup.setIsSubMenu(1);
        guiLayouts.add(popup);

    }

    public void setLeftPopup(GUILayout popup) {

        leftPopup = popup;
        leftPopup.setVisible(0);
        leftPopup.setIsSubMenu(1);
        guiLayouts.add(popup);


    }
}
